package com.project.myxima.data;

import com.project.myxima.interfaces.IHistoryViewCallback;
import com.ximalaya.ting.android.opensdk.model.album.Announcer;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class HistoryDao implements IHistoryDao {

    private IHistoryViewCallback mCallback;


    //SingleInstance
    private HistoryDao() {
    }

    private static class InnerHolder {
        private static final HistoryDao INSTANCE = new HistoryDao();
    }

    public static HistoryDao getInstance() {
        return InnerHolder.INSTANCE;
    }
    //===============================================================


    @Override
    public void setCallback(IHistoryViewCallback callback) {
        mCallback = callback;

    }

    @Override
    public void addHistory(Track track) {
        MyTrack myTrack = track2MyTrack(track);
        //delHistory(track);


        BmobQuery<MyTrack> query = new BmobQuery<>();
        query.addWhereEqualTo("user", BmobUser.getCurrentUser(BmobUser.class));
        query.addWhereEqualTo("dataId", myTrack.getDataId());
        query.order("-updatedAt");
        query.findObjects(new FindListener<MyTrack>() {
            @Override
            public void done(List<MyTrack> list, BmobException e) {
                if(e == null && list.size()>0){
                    myTrack.update(list.get(0).getObjectId(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            mCallback.onAddHistory(e == null);
                        }
                    });
                }else {
                    myTrack.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            mCallback.onAddHistory(e == null);
                        }
                    });
                }
            }
        });



    }

    private MyTrack track2MyTrack(Track track) {
        MyTrack myTrack = new MyTrack();
        myTrack.setCoverUrlLarge(track.getCoverUrlLarge());
        myTrack.setDataId(track.getDataId());
        myTrack.setDuration(track.getDuration());
        myTrack.setNickname(track.getAnnouncer().getNickname());
        myTrack.setPlayCount(track.getPlayCount());
        myTrack.setTrackTitle(track.getTrackTitle());
        myTrack.setUpdatedDate(track.getUpdatedAt());
        myTrack.setUser(BmobUser.getCurrentUser(BmobUser.class));
        return myTrack;

    }

    @Override
    public void delHistory(Track track) {
        MyTrack myTrack = track2MyTrack(track);
        BmobQuery<MyTrack> query = new BmobQuery<>();
        query.addWhereEqualTo("user", BmobUser.getCurrentUser(BmobUser.class));
        query.addWhereEqualTo("dataId", myTrack.getDataId());
        query.order("-updatedAt");

        query.findObjects(new FindListener<MyTrack>() {
            @Override
            public void done(List<MyTrack> list, BmobException e) {
                if (e == null) {
                    if (list.size() > 0) {
                        myTrack.delete(list.get(0).getObjectId(), new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                mCallback.onDelHistory(e == null);
                            }
                        });
                    }
                } else {
                    mCallback.onDelHistory(false);
                }
            }
        });

    }

    @Override
    public void getHistoryList() {
        BmobQuery<MyTrack> query = new BmobQuery<>();

        query.addWhereEqualTo("user", BmobUser.getCurrentUser(BmobUser.class));
        query.order("-updatedAt");
        query.findObjects(new FindListener<MyTrack>() {
            @Override
            public void done(List<MyTrack> list, BmobException e) {
                if (e == null) {
                    List<Track> trackList = new ArrayList<>();
                    trackList.clear();
                    if (list.size() > 0) {
                        for (MyTrack myTrack : list) {
                            Track track = myTrack2Track(myTrack);
                            trackList.add(track);
                        }
                    }
                    mCallback.onHistoryListLoad(trackList);
                } else {
                    //Network error
                    mCallback.onHistoryListLoadError();
                }
            }
        });

    }

    private Track myTrack2Track(MyTrack myTrack) {
        Track track = new Track();
        track.setDataId(myTrack.getDataId());
        track.setDuration(myTrack.getDuration());
        track.setPlayCount(myTrack.getPlayCount());
        track.setTrackTitle(myTrack.getTrackTitle());
        track.setUpdatedAt(myTrack.getUpdatedDate());
        track.setCoverUrlLarge(myTrack.getCoverUrlLarge());
        track.setCoverUrlMiddle(myTrack.getCoverUrlLarge());
        track.setCoverUrlSmall(myTrack.getCoverUrlLarge());

        Announcer announcer = new Announcer();
        announcer.setNickname(myTrack.getNickname());
        track.setAnnouncer(announcer);
        return track;
    }

    @Override
    public void clearHistory() {
        BmobQuery<MyTrack> query = new BmobQuery<>();

        query.addWhereEqualTo("user", BmobUser.getCurrentUser(BmobUser.class));
        query.order("-updatedAt");
        query.findObjects(new FindListener<MyTrack>() {
            @Override
            public void done(List<MyTrack> list, BmobException e) {
                if (e == null) {
                    for (MyTrack myTrack : list) {
                        myTrack.delete(myTrack.getObjectId(), new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                            }
                        });
                    }
                    mCallback.onCleanHistory(true);
                } else {
                    mCallback.onCleanHistory(false);
                }
            }
        });
    }
}
